package cc.rengu.igas.channel.cups.core.service.trans;


import cc.rengu.igas.channel.cups.common.constant.CupsParamConstant;
import cc.rengu.jradp.mods.trmsg.iso8583.MacUtil;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.utility.cache.UnifiedCache;

public class LoadCupsMacConfigService extends RadpService {

    @Override
    protected int bizInit(String s) {
        //修改为加到缓存中
        //缓存的KEY为"CUPS_8583_MAC_"+CupsParamConstant.CUPS_8583_MAC_FILE+"_"+CupsParamConstant.CUPS_8583_MAC_TYPE_COMMON
        MacUtil consumeMac = (MacUtil) UnifiedCache.get(CupsParamConstant.CUPS_8583_MAC_CACHE_NAME,
                CupsParamConstant.CUPS_8583_MAC_FILE +
                        CupsParamConstant.CUPS_8583_MAC_TYPE_COMMON);
        if (null == consumeMac) {
            rglog.info("缓存中没有加载MAC配置FILE={}，TYPE={}",
                    CupsParamConstant.CUPS_8583_MAC_FILE, CupsParamConstant.CUPS_8583_MAC_TYPE_COMMON);
            consumeMac = new MacUtil(CupsParamConstant.CUPS_8583_MAC_FILE, CupsParamConstant.CUPS_8583_MAC_TYPE_COMMON);

            int ret = UnifiedCache.set(CupsParamConstant.CUPS_8583_MAC_CACHE_NAME,
                    CupsParamConstant.CUPS_8583_MAC_FILE +
                            CupsParamConstant.CUPS_8583_MAC_TYPE_COMMON, consumeMac);
            if (0 != ret) {
                rglog.error("设置缓存错误cache name={}, key={}",
                        CupsParamConstant.CUPS_8583_MAC_CACHE_NAME, CupsParamConstant.CUPS_8583_MAC_FILE +
                                CupsParamConstant.CUPS_8583_MAC_TYPE_COMMON);
                return -1;
            }
            rglog.info("缓存中加载MAC配置完成，FILE={}，TYPE={}",
                    CupsParamConstant.CUPS_8583_MAC_FILE, CupsParamConstant.CUPS_8583_MAC_TYPE_COMMON);
        } else {
            rglog.info("缓存中已经加载MAC配置FILE={}，TYPE={}",
                    CupsParamConstant.CUPS_8583_MAC_FILE, CupsParamConstant.CUPS_8583_MAC_TYPE_COMMON);
        }

        MacUtil keyMac = (MacUtil) UnifiedCache.get(CupsParamConstant.CUPS_8583_MAC_CACHE_NAME,
                CupsParamConstant.CUPS_8583_MAC_FILE +
                        CupsParamConstant.CUPS_8583_MAC_TYPE_KEY);
        if (null == keyMac) {
            rglog.info("缓存中没有加载MAC配置FILE={}，TYPE={}",
                    CupsParamConstant.CUPS_8583_MAC_FILE, CupsParamConstant.CUPS_8583_MAC_TYPE_KEY);
            keyMac = new MacUtil(CupsParamConstant.CUPS_8583_MAC_FILE, CupsParamConstant.CUPS_8583_MAC_TYPE_KEY);

            int ret = UnifiedCache.set(CupsParamConstant.CUPS_8583_MAC_CACHE_NAME,
                    CupsParamConstant.CUPS_8583_MAC_FILE +
                            CupsParamConstant.CUPS_8583_MAC_TYPE_KEY, keyMac);
            if (0 != ret) {
                rglog.error("设置缓存错误cache name={}, key={}",
                        CupsParamConstant.CUPS_8583_MAC_CACHE_NAME, CupsParamConstant.CUPS_8583_MAC_FILE +
                                CupsParamConstant.CUPS_8583_MAC_TYPE_KEY);
                return -1;
            }
            rglog.info("缓存中加载MAC配置完成，FILE={}，TYPE={}",
                    CupsParamConstant.CUPS_8583_MAC_FILE, CupsParamConstant.CUPS_8583_MAC_TYPE_KEY);
        } else {
            rglog.info("缓存中已经加载MAC配置FILE={}，TYPE={}",
                    CupsParamConstant.CUPS_8583_MAC_FILE, CupsParamConstant.CUPS_8583_MAC_TYPE_KEY);
        }


        MacUtil taMac = (MacUtil) UnifiedCache.get(CupsParamConstant.CUPS_8583_MAC_CACHE_NAME,
                CupsParamConstant.CUPS_8583_MAC_FILE +
                        CupsParamConstant.CUPS_8583_MAC_TYPE_TA);
        if (null == taMac) {
            rglog.info("缓存中没有加载MAC配置FILE={}，TYPE={}",
                    CupsParamConstant.CUPS_8583_MAC_FILE, CupsParamConstant.CUPS_8583_MAC_TYPE_KEY);
            taMac = new MacUtil(CupsParamConstant.CUPS_8583_MAC_FILE, CupsParamConstant.CUPS_8583_MAC_TYPE_TA);

            int ret = UnifiedCache.set(CupsParamConstant.CUPS_8583_MAC_CACHE_NAME,
                    CupsParamConstant.CUPS_8583_MAC_FILE +
                            CupsParamConstant.CUPS_8583_MAC_TYPE_TA, taMac);
            if (0 != ret) {
                rglog.error("设置缓存错误cache name={}, key={}",
                        CupsParamConstant.CUPS_8583_MAC_CACHE_NAME, CupsParamConstant.CUPS_8583_MAC_FILE +
                                CupsParamConstant.CUPS_8583_MAC_TYPE_TA);
                return -1;
            }
            rglog.info("缓存中加载MAC配置完成，FILE={}，TYPE={}",
                    CupsParamConstant.CUPS_8583_MAC_FILE, CupsParamConstant.CUPS_8583_MAC_TYPE_TA);

        } else {
            rglog.info("缓存中已经加载MAC配置FILE={}，TYPE={}",
                    CupsParamConstant.CUPS_8583_MAC_FILE, CupsParamConstant.CUPS_8583_MAC_TYPE_TA);
        }

        return 0;
    }

    @Override
    protected int bizProcess(String s, int i) {
        return 0;
    }

    @Override
    protected int bizTerminate(String s) {
        return 0;
    }
}
